package com.zzdx.deployment.web.controller.msg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zzdx.deployment.framework.web.domain.server.Sys;
import org.apache.http.NameValuePair;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wenhan(Mecera)
 * @version V1.0
 * @Title: test
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 12:47 AM
 */
public class MSGUtil {

    public static boolean isPhoneNum(String phone){
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
                if(phone.length() != 11){
                       return false;
                    }else{
                        Pattern p = Pattern.compile(regex);
                        Matcher m = p.matcher(phone);
                        boolean isMatch = m.matches();
                        if(isMatch){
                                return true;
                            } else {
                                 return  false;
                        }
               }
    }
    public static String createRandomVcode(){
        //验证码
        String vcode = "";
        for (int i = 0; i < 4; i++) {
            vcode = vcode + (int)(Math.random() * 9); }
        return vcode;
    }

    public static boolean SendMsG(String phone,String message) throws Exception {

        String phones=phone;
        String mhtMsgIds="1";
        String msgType = "1";
        String msgContent=message;
        String url = "http://120.55.193.51:8098/smtp/http/submit";
        String userName = "cbszwfw";
        String userPassword = "cbszwfw@123";
        String serviceCode = "cbszwfw";
        String timestamp = getTime();
        String sendTime = "";
        String priority = "5";
        String orgCode = "";
        String extSubNum  = "";
        String clientStr = "";

        clientStr = msgContent;
        String reportFlag = "0";
        String sign = md5(userPassword+timestamp).toUpperCase();
        Object [] params = new Object[]{"timestamp","userName","sign","serviceCode",
                "phones","mhtMsgIds","sendTime","priority","orgCode","msgType","msgContent","reportFlag"};
        clientStr = java.net.URLEncoder.encode(clientStr );
        Object [] values = new Object[]{timestamp,userName,sign,serviceCode,phones,mhtMsgIds,sendTime,priority,
                orgCode,msgType,clientStr,reportFlag};

        List<NameValuePair> paramsList = HttpClientUtil.getParams(params, values);
        com.alibaba.fastjson.JSONObject result2 = (JSONObject) HttpClientUtil.sendPost(url, paramsList);
        Integer a= result2.getInteger("result");
        System.out.println(result2);
        return a==0;
    }
    public static void main(String[] args) throws Exception {
        boolean a= SendMsG("15739576242","啦啦啦张三");
        System.out.println(a);
    }


    public static String md5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getTime() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String formatTime = formatter.format(date);
        return formatTime;
    }
}